package com.skeleton.server.blog.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.db.dto.BlogArticleDto;
import com.skeleton.db.dto.BlogCommentDto;
import com.skeleton.db.dto.BlogTagDto;
import com.skeleton.db.dto.BlogTagUserDto;
import com.skeleton.db.dto.UserinfoDto;
import com.skeleton.server.blog.service.IBlogService;
import com.skeleton.server.user.dto.BlogPublishDto;
import com.skeleton.utils.SystemConstants;
import com.skeleton.utils.SystemTools;

@Controller
@RequestMapping("blog/")
public class BlogControl {

	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private IBlogService iBlogService;

	@RequestMapping("publish")
	@ResponseBody
	public Map<String, Object> publish(HttpServletRequest request, HttpSession session) {
		logger.info("-------blog---publish-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String tag = request.getParameter("tag");

		UserinfoDto ud = (UserinfoDto) session.getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);

		BlogPublishDto bpDto = new BlogPublishDto();

		BlogArticleDto baDto = new BlogArticleDto();
		baDto.setBlogId(SystemTools.getUUIDString());
		baDto.setLoginname(ud.getUserLoginname());
		baDto.setBlogTitle(title);
		baDto.setBlogContent(content);
		baDto.setBlogTags(tag);
		baDto.setBlogCreateDttm(SystemTools.convertCurrentDTTMtoInt());
		baDto.setBlogCreateIp(request.getRemoteAddr());
		baDto.setBlogLastModifyDttm(SystemTools.convertCurrentDTTMtoInt());
		baDto.setBlogVisitCnt(0);
		baDto.setBlogFavourCnt(0);
		baDto.setBlogCollectCnt(0);
		baDto.setBlogStatus(SystemConstants.BLOG_STATUS_NORMAL);
		baDto.setBlogSeq(0);
		bpDto.setBad(baDto);

		List<BlogTagDto> btdList = new ArrayList<BlogTagDto>();
		String[] tagArr = tag.split(",");
		for (int i = 0; i < tagArr.length; i++) {
			String tagname = tagArr[i];
			BlogTagDto btDto = new BlogTagDto();
			btDto.setTagId(0);
			btDto.setTagname(tagname);
			btDto.setTagType(SystemConstants.BLOG_TAG_TYPE_USER);
			btDto.setTagUseCnt(1);
			btDto.setTagStatus(SystemConstants.BLOG_TAG_STATUS_NORMAL);
			btDto.setTagSeq(0);
			btdList.add(btDto);
		}
		bpDto.setBtdList(btdList);

		List<BlogTagUserDto> btudList = new ArrayList<BlogTagUserDto>();
		for (int i = 0; i < tagArr.length; i++) {
			String tagname = tagArr[i];
			BlogTagUserDto btuDto = new BlogTagUserDto();
			btuDto.setTagUserId(0);
			btuDto.setLoginname(ud.getUserLoginname());
			btuDto.setTagname(tagname);
			btuDto.setTagSeq(0);
			btudList.add(btuDto);
		}
		bpDto.setBtudList(btudList);

		ResultDto rd = iBlogService.doBlogPublish(bpDto);
		if (rd.isSuccess()) {
			map.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			map.put("result", SystemConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("getBlogArticleByPage")
	@ResponseBody
	public Map<String, Object> getBlogArticleByPage(HttpServletRequest request) {
		logger.info("-------blog---getBlogArticleByPage-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String page = request.getParameter("page");
		int pageNum = 0;
		try {
			pageNum = Integer.parseInt(page);
		} catch (NumberFormatException e) {
			pageNum = 0;
		}

		ResultDto rd = iBlogService.getBlogArticleListByPage(pageNum);
		if (rd.isSuccess()) {
			List<BlogArticleDto> badList = rd.getDtoList("BlogArticleDtoList");
			map.put("badList", badList);
			int total = (Integer) rd.getOther("total");
			map.put("total", total);
			map.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			map.put("result", SystemConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("getArticleById")
	@ResponseBody
	public Map<String, Object> getArticleById(HttpServletRequest request) {
		logger.info("-------blog---getArticleById-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String article_id = request.getParameter("article_id");

		BlogArticleDto baDto = new BlogArticleDto();
		baDto.setBlogId(article_id);

		ResultDto rd = iBlogService.getBlogArticleById(baDto);
		if (rd.isSuccess()) {
			BlogArticleDto bad = (BlogArticleDto) rd.getFirstDto("BlogArticleDto");
			map.put("bad", bad);
			map.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			map.put("result", SystemConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("addBlogArticleVisitCnt")
	@ResponseBody
	public Map<String, Object> addBlogArticleVisitCnt(HttpServletRequest request) {
		logger.info("-------blog---addBlogArticleVisitCnt-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String article_id = request.getParameter("article_id");

		BlogArticleDto baDto = new BlogArticleDto();
		baDto.setBlogId(article_id);

		ResultDto rd = iBlogService.doAddBlogArticleVisitCnt(baDto);
		if (rd.isSuccess()) {
			map.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			map.put("result", SystemConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("addBlogArticleFavourCnt")
	@ResponseBody
	public Map<String, Object> addBlogArticleFavourCnt(HttpServletRequest request) {
		logger.info("-------blog---addBlogArticleFavourCnt-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String article_id = request.getParameter("article_id");

		BlogArticleDto baDto = new BlogArticleDto();
		baDto.setBlogId(article_id);

		ResultDto rd = iBlogService.doAddBlogArticleFavourCnt(baDto);
		if (rd.isSuccess()) {
			map.put("article_id", article_id);
			map.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			map.put("result", SystemConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

}
